import QtQuick
import QtQuick.Dialogs

QtObject {
    id: root
    
    // 创建消息框组件
    property var messageBox: null
    required property QtObject parent

    // 显示信息类消息
    function showInfo(title, message) {
        createAndShow("info", title, message)
    }
    
    // 显示警告消息
    function showWarning(title, message) {
        createAndShow("warning", title, message)
    }
    
    // 显示错误消息
    function showError(title, message) {
        createAndShow("error", title, message)
    }
    
    // 显示成功消息
    function showSuccess(title, message) {
        createAndShow("success", title, message)
    }
    
    // 显示问题消息
    function showQuestion(title, message) {
        return createAndShow("question", title, message)
    }
    
    // 创建并显示消息框
    function createAndShow(type, title, message) {
        if (messageBox !== null) {
            messageBox.destroy()
        }
        
        var component = Qt.createComponent("CustomMessageBox.qml")
        if (component.status === Component.Ready) {
            messageBox = component.createObject(parent, {
                "messageType": type,
                "messageTitle": title,
                "messageText": message
            })
            
            messageBox.accepted.connect(function() {
                messageBox.destroy()
                messageBox = null
            })
            
            messageBox.rejected.connect(function() {
                messageBox.destroy()
                messageBox = null
            })
            
            messageBox.open()
            return messageBox
        } else if (component.status === Component.Error) {
            console.error("Error creating component:", component.errorString())
            return null
        }
    }

    //这里是展示自带的消息框用法
    function showQtMessageBox() {
        var dialog = Qt.createQmlObject('
            import QtQuick.Dialogs
            MessageDialog {
                title: "消息标题"
                text: "这是消息内容"
                buttons: MessageDialog.Ok
                onAccepted: console.log("OK button clicked")
                onRejected: console.log("Cancel button clicked")
            }
        ', root)
        dialog.open()
    }
}
